import 'package:flutter/material.dart';
import 'package:paperless_api/paperless_api.dart';
import 'package:paperless_mobile/features/settings/view/widgets/biometric_authentication_setting.dart';
import 'package:paperless_mobile/features/settings/view/widgets/clear_storage_settings.dart';
import 'package:paperless_mobile/features/settings/view/widgets/color_scheme_option_setting.dart';
import 'package:paperless_mobile/features/settings/view/widgets/default_download_file_type_setting.dart';
import 'package:paperless_mobile/features/settings/view/widgets/default_share_file_type_setting.dart';
import 'package:paperless_mobile/features/settings/view/widgets/enforce_pdf_upload_setting.dart';
import 'package:paperless_mobile/features/settings/view/widgets/language_selection_setting.dart';
import 'package:paperless_mobile/features/settings/view/widgets/theme_mode_setting.dart';
import 'package:paperless_mobile/features/settings/view/widgets/user_settings_builder.dart';
import 'package:paperless_mobile/generated/l10n/app_localizations.dart';
import 'package:provider/provider.dart';

class SettingsPage extends StatelessWidget {
  const SettingsPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(S.of(context)!.settings),
      ),
      body: ListView(
        children: [
          _buildSectionHeader(context, S.of(context)!.appearance),
          const LanguageSelectionSetting(),
          const ThemeModeSetting(),
          const ColorSchemeOptionSetting(),
          _buildSectionHeader(context, S.of(context)!.security),
          const BiometricAuthenticationSetting(),
          _buildSectionHeader(context, S.of(context)!.behavior),
          const DefaultDownloadFileTypeSetting(),
          const DefaultShareFileTypeSetting(),
          const EnforcePdfUploadSetting(),
          _buildSectionHeader(context, S.of(context)!.storage),
          const ClearCacheSetting(),
        ],
      ),
      bottomNavigationBar: UserAccountBuilder(
        builder: (context, user) {
          assert(user != null);
          final host = user!.serverUrl.replaceFirst(RegExp(r"https?://"), "");
          return ListTile(
            title: Text(
              S.of(context)!.loggedInAs(user.paperlessUser.username) + "@$host",
              style: Theme.of(context).textTheme.labelSmall,
              textAlign: TextAlign.center,
            ),
            subtitle: FutureBuilder<PaperlessServerInformationModel>(
              future: context
                  .read<PaperlessServerStatsApi>()
                  .getServerInformation(),
              builder: (context, snapshot) {
                if (snapshot.hasError) {
                  return Text(
                    S.of(context)!.errorRetrievingServerVersion,
                    style: Theme.of(context)
                        .textTheme
                        .labelSmall
                        ?.copyWith(color: Theme.of(context).colorScheme.error),
                    textAlign: TextAlign.center,
                  );
                }
                if (!snapshot.hasData) {
                  return Text(
                    S.of(context)!.resolvingServerVersion,
                    style: Theme.of(context).textTheme.labelSmall,
                    textAlign: TextAlign.center,
                  );
                }
                final serverData = snapshot.data!;
                return Text(
                  S.of(context)!.paperlessServerVersion +
                      ' ' +
                      serverData.version.toString() +
                      ' (API v${serverData.apiVersion})',
                  style: Theme.of(context).textTheme.labelSmall?.copyWith(
                        color: Theme.of(context).colorScheme.secondary,
                      ),
                  textAlign: TextAlign.center,
                );
              },
            ),
          );
        },
      ),
    );
  }

  Widget _buildSectionHeader(BuildContext context, String text) {
    return Padding(
      padding: const EdgeInsets.only(left: 16, top: 16),
      child: Text(
        text,
        style: Theme.of(context)
            .textTheme
            .labelLarge
            ?.copyWith(color: Theme.of(context).colorScheme.primary),
      ),
    );
  }

  void _goto(Widget page, BuildContext context) {
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => page,
        maintainState: true,
      ),
    );
  }
}
